﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using OgilvyOne.Model;
using OgilvyOne.IDAL;
using OgilvyOne.DALFactory;
using OgilvyOne.Utility;


namespace OgilvyOne.BLL
{
    public class CmsArticleBll
    {
        /// <summary>
        /// 查询文章
        /// </summary>
        /// <param name="caq"></param>
        /// <returns></returns>
        public IList getCmsArticleList(CmsArticleQuery caq)
        {
            return CmsArticleFactory.create().getCmsArticleList(caq);
        }
        /// <summary>
        /// 批量删除文章
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DelCmsArticle(string ids)
        {
            if (ids != null && ids != "")
            {
                string[] id = ids.Split(',');
                CmsArticleModel model = GetCmsArticleModel(int.Parse(id[0]));
                if (CmsArticleFactory.create().Del(ids))
                {

                    //--------需文章重新生成
                    //if (CmsArticleFrontFactory.create().Del(ids))
                    {
                        SaveColumnHtml(new MenuModel().GetColumn(model.Columnid));
                    }
                    return true;
                }
            }
            return false;
        }
        /// <summary>
        /// 获取单篇文章
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public CmsArticleModel GetCmsArticleModel(int aid)
        {
            return CmsArticleFactory.create().getCmsArticle(aid);
        }
        /// <summary>
        /// 新增文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool Add(CmsArticleModel article)
        {
            return CmsArticleFactory.create().Insert(article);
        }
        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="article"></param>
        /// <returns></returns>
        public bool Update(CmsArticleModel article)
        {
            return CmsArticleFactory.create().Update(article);
        }
        /// <summary>
        /// 发布
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public bool Pubone(int aid, string pubuname)
        {
            if (CmsArticleFactory.create().UpdateState(aid, "1", pubuname))
            {
                CmsArticleModel cm = CmsArticleFactory.create().getCmsArticle(aid);

                CmsArticleFrontFactory.create().PushFront(cm);

                 //-----需生成前台文章
                 CmsArticleModel model = GetCmsArticleModel(aid);
                 MenuModel menuModel = new MenuModel(model.Columnid);
                 SaveColumnHtml(menuModel.GetColumn(model.Columnid));
                 string html = CreateArticleHtml(model, menuModel.GetLayerDir(menuModel.GetColumn().Layerid));
                
                 //添加进搜索
                 string layerid = menuModel.GetColumn().Layerid;
                 LuceneSearchUtil lu = LuceneSearchUtil.GetLuecenSearchUtil(LuceneSearchUtil.GetType(layerid), System.Web.HttpContext.Current.Server.MapPath("~/"), false);
                 LuceneSearchDoc doc = new LuceneSearchDoc();
                 doc.Columnid = model.Columnid.ToString();
                 doc.Content = html;
                 doc.Layerid = layerid;
                 doc.Ttile = model.Title;
                 doc.Url = menuModel.GetArticleUrl(model);
                 doc.Filename = MenuModel.GetAritcleHtmlFileName(model);
                 lu.AddDoc(doc);
                return true;
            }
            return false;
        }
        /// <summary>
        /// 批量发布
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Pubs(string ids, string pubuname)
        {
            string[] idss = ids.Split(',');
            if (idss.Length > 0)
            {

                int aid = int.Parse(idss[0]);

                CmsArticleModel model = GetCmsArticleModel(aid);
                MenuModel menuModel = new MenuModel(model.Columnid);
                string dir = menuModel.GetLayerDir(menuModel.GetColumn().Layerid);

                //SaveColumnHtml(menuModel.GetColumn());

                foreach (string id in idss)
                {
                    if (id != null && id != "" && id != ",")
                    {
                        Pubone(int.Parse(id), pubuname);

                        //CreateArticleHtml(model, dir);
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 批量审核
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool Audits(string ids, string state, string auditname)
        {
            string[] idss = ids.Split(',');
            foreach (string id in idss)
            {
                if (id != null && id != "" && id != ",")
                {
                    Audit(int.Parse(id), state, auditname);
                }
            }

            return true;
        }
        /// <summary>
        /// 审核文章
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public bool Audit(int aid, string state, string auditname)
        {
            return CmsArticleFactory.create().AudutArticle(aid, state, auditname);
        }
        /// <summary>
        /// 撤消发布
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="state"></param>
        /// <param name="pubname"></param>
        /// <returns></returns>
        public bool RePubOne(int aid, string pubname)
        {
            if (CmsArticleFactory.create().UpdateState(aid, "6", pubname))
            {
                CmsArticleFrontFactory.create().Del(aid.ToString());
                CmsArticleModel model = GetCmsArticleModel(aid);
                SaveColumnHtml(new MenuModel().GetColumn(model.Columnid));
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改文章SEO
        /// </summary>
        /// <param name="seo"></param>
        /// <returns></returns>
        public bool UpdateSeo(CmsArticleModel seo)
        {
            if (CmsArticleFactory.create().UpdateSeo(seo))
            {
                return CmsArticleFactory.create().UpdateState(seo.Articleid, "0", seo.Modifyusername);
            }
            return false;
        }
        /// <summary>
        /// 获取文章SEO属性
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public CmsArticleModel GetCmsArticelSeo(int aid)
        {
            return CmsArticleFactory.create().GetCmsArticelSeo(aid);
        }

        public bool UpdateAudit(int aid, string username)
        {
            return CmsArticleFactory.create().UpdateState(aid, "0", username);
        }


        /// <summary>
        /// 修改排序号
        /// </summary>
        /// <param name="aid"></param>
        /// <param name="next">0 上 1 下</param>
        /// <returns>修改排序号</returns>
        public int ModOrderNum(int aid, int next)
        {
            CmsArticleModel cm = GetCmsArticleModel(aid);
            int ordernum = cm.OrderNum;
            int temcmid = CmsArticleFactory.create().GetOrderArticleId(cm.Columnid, ordernum, next);
            if (temcmid > 0)
            {
                CmsArticleModel temcm = CmsArticleFactory.create().getCmsArticle(temcmid);
                CmsArticleFactory.create().ModOrderNum(aid, temcm.OrderNum);
                CmsArticleFactory.create().ModOrderNum(temcm.Articleid, ordernum);

                return temcm.OrderNum;
            }
            return 0;

        }

        /// <summary>
        /// 栏目最大排序号
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public int MaxOrder(int cid)
        {
            return CmsArticleFactory.create().MaxOrder(cid);
        }



        /// <summary>
        /// 生成HTML
        /// </summary>
        /// <param name="model"></param>
        public string CreateArticleHtml(CmsArticleModel model, string dir)
        {
            string html = "";
            if (model.Templateid > 0 && model.Isstaticpub == CmsConstants.YES.ToString())
            {//是生成HTML
                CmsVo vo = new CmsVo();
                vo.Aid = model.Articleid;
                vo.IsHtmlFile = true;
                html = CmsProxy.GetHtml(vo);
                string filename = System.Web.HttpContext.Current.Server.MapPath("~" + dir + "/") + MenuModel.GetAritcleHtmlFileName(model);
                FileUtil.SaveHtmlFile(filename, html);
            }

            return html;
        }
        /// <summary>
        /// 栏目首页生成HTML
        /// </summary>
        /// <param name="model"></param>
        public string CreateColumnHtml(ColumnModel model)
        {
            MenuModel menuModel = new MenuModel(model.Classid);
            string html = CreateColumnIndexHtml(model, menuModel.GetLayerDir(model.Layerid));

            if (model.Is_class_index == CmsConstants.YES)
            {//生成各层级栏目首页
                string[] ms = model.Layerid.Split(',');
                if (ms.Length > 2)
                {
                    for (int i = 2; i < ms.Length - 1; i++)
                    {
                        ColumnModel cm = menuModel.GetColumn(int.Parse(ms[i]));
                        CreateColumnIndexHtml(cm, menuModel.GetLayerDir(cm.Layerid));
                    }
                }
            }
            if (model.Is_index == CmsConstants.YES)
            {//生成首页
                if (model.Layerid.IndexOf(",2,") > -1)
                {//中文各首页
                    string dir = menuModel.GetLayerDir(menuModel.GetColumn(ConConstants.HOME_ID).Layerid);
                    CreateColumnIndexHtml(menuModel.GetColumn(ConConstants.HOME_ID), dir);
                    IList alist = GetColumnAllPubArticleList(ConConstants.HOME_ID);
                    for (int i = 0; i < alist.Count; i++)
                    {//生成各通道首页
                        CreateArticleHtml((CmsArticleModel)alist[i], dir);
                    }
                }
                else
                {//英文各首页

                }
            }
            if (model.Is_content_navi == CmsConstants.YES)
            {//内容导航栏目
                int mainid = menuModel.GetMainColumnId();
                //IList mlist = menuModel.GetLayeridColumn(mainid);
                //for (int m = 0; m < mlist.Count; m++)
                {
                    ColumnModel cm = menuModel.GetColumn();
                    IList alist = GetColumnAllPubArticleList(cm.Classid);
                    string dir = menuModel.GetLayerDir(cm.Layerid);
                    for (int i = 0; i < alist.Count; i++)
                    {
                        CreateArticleHtml((CmsArticleModel)alist[i], dir);
                    }
                }
            }
            return html;
        }
        public string CreateColumnIndexHtml(ColumnModel model, string dir)
        {
            string html = "";
            if (model.Class_is_static == CmsConstants.YES)
            {
                if (model.Templateindex > 0)
                {
                    CmsVo vo = new CmsVo();
                    vo.Cid = model.Classid;
                    vo.IsHtmlFile = true;
                    html = CmsProxy.GetHtml(vo);
                    string filename = System.Web.HttpContext.Current.Server.MapPath("~" + dir + "/") + MenuModel.GetColumnHtmlFileName(model);
                    FileUtil.SaveHtmlFile(filename, html);
                }
            }
            return html;
        }
        //栏目相关HTML生成
        public void SaveColumnHtml(ColumnModel cm)
        {
            CreateColumnHtml(cm);
            //内容变更生成对应栏目标签HTML
            new TemplateLabelBll().ColumnSaveLabelsHtml(cm.Html_tags);
        }
        /// <summary>
        /// 栏目相关HTML生成
        /// </summary>
        /// <param name="layerid"></param>
        public void SaveColumnHtml(string layerid)
        {
            if (layerid != null)
            {
                string[] ls = layerid.Split(',');
                int cid = int.Parse(ls[ls.Length - 1]);
                SaveColumnHtml(new MenuModel(cid).GetColumn());
            }
        }

        /// <summary>
        /// 栏目发布文章列表
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public IList GetColumnAllPubArticleList(int cid)
        {
            return CmsArticleFrontFactory.create().GetArticleOrderList(cid, 0);
        }

        /// <summary>
        /// 所有层级栏目发布文章列表
        /// </summary>
        /// <param name="layerid"></param>
        /// <param name="row"></param>
        /// <returns></returns>
        public IList GetLayerPubArticleList(string layerid, int row)
        {
            return CmsArticleFrontFactory.create().GetLayerArticleList(layerid, row);
        }

        public bool AddHits(int aid)
        {
            return CmsArticleFrontFactory.create().addHits(aid);
        }
        /// <summary>
        /// 阅读数
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public int GetHits(int aid)
        {
            return CmsArticleFrontFactory.create().GetArticleHits(aid);
        }
        /// <summary>
        /// 收藏数
        /// </summary>
        /// <param name="aid"></param>
        /// <returns></returns>
        public int GetColls(int aid)
        {
            return CmsArticleFrontFactory.create().GetArticleColls(aid);
        }
        ///// <summary>
        ///// 相关业务查询
        ///// </summary>
        ///// <param name="aid"></param>
        ///// <returns></returns>
        //public CmsArticleModel GetSubsidiary(int aid)
        //{
        //    return CmsArticleFrontFactory.create().GetSubsidiary(aid);
        //}
    }
}
